/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.log.operate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.siufung.market.base.log.login.controller.model.dto.page.OperateLogPageDto;
import net.siufung.market.base.log.login.mapper.entity.OperateLog;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.page.PagerResult;
import net.siufung.core.result.Result;
import net.siufung.market.base.log.operate.controller.assembler.OperateLogAssembler;
import net.siufung.market.base.log.operate.controller.model.vo.OperateLogVo;
import net.siufung.market.base.log.operate.service.IOperateLogService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统日志表 前端控制器
 *
 * @author 陈建峰
 * @since 2022-07-04 17:54:53
 */
@Api(tags = "系统日志")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/log/operate")
public class OperateLogController {

    private final IOperateLogService operateLogService;

    /**
     * 通过主键获取 系统日志表
     * @param id 主键
     * @return Result<OperateLogVo>
     * */
    @ApiOperation(value = "通过Id获取系统日志表信息")
    @GetMapping(value = "/id/{id}")
    public Result<OperateLogVo> getById(
            @PathVariable("id") Long id) {
        return Result.ok(
            OperateLogAssembler.from(operateLogService.getById(id)));
    }

    /**
     * 获取 系统日志表分页
     * @param logPageDto 属性
     * @return 查询分页结果
     * */
    @ApiOperation(value = "获取系统日志表分页")
    @PostMapping(value="/page")
    public Result<PagerResult<OperateLogVo>> getPage(
            @RequestBody OperateLogPageDto logPageDto) {
        logPageDto.validated();
        QueryWrapper<OperateLog> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(
                !Strings.isNullOrEmpty(logPageDto.getLoginName()), OperateLog::getLoginName, logPageDto.getLoginName());
        wrapper.lambda().like(
                !Strings.isNullOrEmpty(logPageDto.getTitle()), OperateLog::getTitle, logPageDto.getTitle());
        wrapper.lambda().orderByDesc(OperateLog::getCreatedAt);
        IPage<OperateLog> logPage = operateLogService.page(
                logPageDto.getPageNo(), logPageDto.getPageSize(), wrapper);
        PagerResult<OperateLogVo> logVoPage = new PagerResult<>(
                logPage.getCurrent(), logPage.getSize(), logPage.getTotal());
        logVoPage.setRecords(
                OperateLogAssembler.fromList(logPage.getRecords()));
        return Result.ok(logVoPage);
    }

}